package com.bw.a2207a.view

import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.a2207a.R
import com.bw.a2207a.base.BaseActivity
import com.bw.a2207a.bean.CartBean
import com.bw.a2207a.databinding.ActivityCartListBinding
import com.bw.a2207a.view.adapter.CartAdapter2
import com.bw.a2207a.viewmodel.GoodsDetailViewModel

class CartListActivity :
    BaseActivity<ActivityCartListBinding, GoodsDetailViewModel>(R.layout.activity_cart_list) {

    lateinit var adapter: CartAdapter2
    val datas = mutableListOf<CartBean>()
    var count = 0
    override fun initData() {
        viewModel.goodsCarsLiveData.observe(this) {
            if (it.code == 200) {
                if (it.data.isNotEmpty()) {
                    datas.clear()
                    datas.addAll(it.data)
                    adapter.notifyDataSetChanged()

                    //设置总数量及总价格
                    datas.forEach() {
                        //count = count+it.count
                        count += it.count * it.goods_default_price//数量相加
                    }
                    binding.tvCount.text = "总价：${count} 元"
                }
            }
        }
    }

    override fun initView() {
        viewModel.getCar()
        binding.rv.layoutManager = LinearLayoutManager(this)
        adapter = CartAdapter2(R.layout.item_cart_layout2, datas)
        binding.rv.adapter = adapter
    }

}